/*   1:    */ package org.intellij.plugins.relaxNG.model.descriptors;
/*   2:    */ 
/*   3:    */

import com.intellij.codeInsight.daemon.Validator;
import com.intellij.codeInsight.daemon.Validator.ValidationHost;
import com.intellij.javaee.ExternalResourceManager;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.ModificationTracker;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.PsiElementProcessor.CollectElements;
import com.intellij.psi.util.*;
import com.intellij.psi.util.CachedValueProvider.Result;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.HashMap;
import com.intellij.xml.XmlElementDescriptor;
import com.intellij.xml.XmlNSDescriptorEx;
import com.intellij.xml.impl.schema.AnyXmlElementDescriptor;
import org.intellij.plugins.relaxNG.model.resolve.RelaxIncludeIndex;
import org.intellij.plugins.relaxNG.validation.RngParser;
import org.intellij.plugins.relaxNG.validation.XmlInstanceValidator;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kohsuke.rngom.digested.DElementPattern;
import org.kohsuke.rngom.digested.DPattern;
import org.kohsuke.rngom.nc.NameClass;

import javax.xml.namespace.QName;
import java.util.*;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */
/*  37:    */
/*  38:    */
/*  39:    */
/*  40:    */
/*  41:    */
/*  42:    */
/*  43:    */
/*  44:    */
/*  45:    */
/*  46:    */
/*  47:    */
/*  48:    */
/*  49:    */
/*  50:    */ 
/*  51:    */ public class RngNsDescriptor
/*  52:    */   implements XmlNSDescriptorEx, Validator
/*  53:    */ {
/*  54: 61 */   private final Map<QName, CachedValue<XmlElementDescriptor>> a = Collections.synchronizedMap(new HashMap());
/*  55: 64 */   private static final Key<ParameterizedCachedValue<XmlElementDescriptor, RngNsDescriptor>> b = Key.create("ROOT_DESCRIPTOR");
/*  56:    */   private XmlFile c;
/*  57:    */   private PsiElement d;
/*  58:    */   private String e;
/*  59:    */   private DPattern f;
/*  60:    */   private PsiManager g;
/*  61:    */   
/*  62:    */   @Nullable
/*  63:    */   public XmlElementDescriptor getElementDescriptor(@NotNull XmlTag paramXmlTag)
/*  64:    */   {
/*  65: 75 */     if (paramXmlTag == null) {
/*  66: 75 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/model/descriptors/RngNsDescriptor", "getElementDescriptor" }));
/*  67:    */     }
/*  68: 75 */     if (this.f == null) {
/*  69: 76 */       return null;
/*  70:    */     }
/*  71: 79 */     XmlTag localXmlTag1 = paramXmlTag;
/*  72: 80 */     LinkedList localLinkedList = new LinkedList();
/*  73: 81 */     while (localXmlTag1 != null)
/*  74:    */     {
/*  75: 82 */       localLinkedList.addFirst(localXmlTag1);
/*  76: 83 */       localXmlTag1 = localXmlTag1.getParentTag();
/*  77:    */     }
/*  78:    */     XmlElementDescriptor localXmlElementDescriptor;
/*  79:    */     do
/*  80:    */     {
/*  81: 88 */       localXmlElementDescriptor = a((XmlTag)localLinkedList.removeFirst());
/*  82: 89 */     } while ((localXmlElementDescriptor == null) && (localLinkedList.size() > 0));
/*  83: 91 */     if (localXmlElementDescriptor != null) {
/*  84: 92 */       for (XmlTag localXmlTag2 : localLinkedList)
/*  85:    */       {
/*  86: 93 */         localXmlElementDescriptor = localXmlElementDescriptor.getElementDescriptor(localXmlTag2, localXmlTag2.getParentTag());
/*  87: 94 */         if (localXmlElementDescriptor == null) {
/*  88:    */           break;
/*  89:    */         }
/*  90:    */       }
/*  91:    */     }
/*  92:100 */     if ((localXmlElementDescriptor == null) || ((localXmlElementDescriptor instanceof AnyXmlElementDescriptor))) {
/*  93:101 */       return a(paramXmlTag);
/*  94:    */     }
/*  95:104 */     return localXmlElementDescriptor;
/*  96:    */   }
/*  97:    */   
/*  98:    */   private XmlElementDescriptor a(final XmlTag paramXmlTag)
/*  99:    */   {
/* 100:108 */     (XmlElementDescriptor)CachedValuesManager.getManager(paramXmlTag.getProject()).getParameterizedCachedValue(paramXmlTag, b, new ParameterizedCachedValueProvider()
/* 101:    */     {
/* 102:    */       public CachedValueProvider.Result<XmlElementDescriptor> compute(RngNsDescriptor paramAnonymousRngNsDescriptor)
/* 103:    */       {
/* 104:111 */         XmlElementDescriptor localXmlElementDescriptor = paramAnonymousRngNsDescriptor.b(paramXmlTag);
/* 105:112 */         if (localXmlElementDescriptor != null) {
/* 106:113 */           return CachedValueProvider.Result.create(localXmlElementDescriptor, new Object[] { paramXmlTag, localXmlElementDescriptor.getDependences(), paramAnonymousRngNsDescriptor.getDependences() });
/* 107:    */         }
/* 108:116 */         return CachedValueProvider.Result.create(null, new Object[] { paramXmlTag, paramAnonymousRngNsDescriptor.getDependences() });
/* 109:    */       }
/* 110:116 */     }, false, this);
/* 111:    */   }
/* 112:    */   
/* 113:    */   private XmlElementDescriptor b(XmlTag paramXmlTag)
/* 114:    */   {
/* 115:123 */     List localList = ContainerUtil.findAll(ChildElementFinder.find(-1, this.f), NamedPatternFilter.INSTANCE);
/* 116:    */     
/* 117:125 */     XmlElementDescriptor localXmlElementDescriptor = findDescriptor(paramXmlTag, localList);
/* 118:126 */     return localXmlElementDescriptor != null ? localXmlElementDescriptor : findDescriptor(paramXmlTag, ChildElementFinder.find(this.f));
/* 119:    */   }
/* 120:    */   
/* 121:    */   private XmlElementDescriptor a(QName paramQName)
/* 122:    */   {
/* 123:130 */     return a(paramQName, ContainerUtil.findAll(ChildElementFinder.find(-1, this.f), NamedPatternFilter.INSTANCE));
/* 124:    */   }
/* 125:    */   
/* 126:    */   public XmlElementDescriptor findDescriptor(XmlTag paramXmlTag, List<DElementPattern> paramList)
/* 127:    */   {
/* 128:135 */     QName localQName = new QName(paramXmlTag.getNamespace(), paramXmlTag.getLocalName());
/* 129:    */     
/* 130:137 */     return a(localQName, paramList);
/* 131:    */   }
/* 132:    */   
/* 133:    */   private XmlElementDescriptor a(final QName paramQName, List<DElementPattern> paramList)
/* 134:    */   {
/* 135:141 */     int i = -1;
/* 136:142 */     Object localObject1 = null;
/* 137:143 */     for (Object localObject2 = paramList.iterator(); ((Iterator)localObject2).hasNext();)
/* 138:    */     {
/* 139:143 */       DElementPattern localDElementPattern = (DElementPattern)((Iterator)localObject2).next();
/* 140:144 */       NameClass localNameClass = localDElementPattern.getName();
/* 141:145 */       if (localNameClass.contains(paramQName))
/* 142:    */       {
/* 143:146 */         int j = localNameClass.containsSpecificity(paramQName);
/* 144:147 */         if (j > i)
/* 145:    */         {
/* 146:148 */           localObject1 = localDElementPattern;
/* 147:149 */           i = j;
/* 148:    */         }
/* 149:    */       }
/* 150:    */     }
/* 151:153 */     localObject2 = ContainerUtil.findAll(paramList, new Condition()
/* 152:    */     {
/* 153:    */       public boolean value(DElementPattern paramAnonymousDElementPattern)
/* 154:    */       {
/* 155:155 */         NameClass localNameClass = paramAnonymousDElementPattern.getName();
/* 156:156 */         return localNameClass.contains(paramQName);
/* 157:    */       }
/* 158:    */     });
/* 159:160 */     if (localObject1 != null)
/* 160:    */     {
/* 161:161 */       if (((List)localObject2).size() > 1) {
/* 162:162 */         return initDescriptor(new CompositeDescriptor(this, localObject1, (List)localObject2));
/* 163:    */       }
/* 164:164 */       return initDescriptor(new RngElementDescriptor(this, localObject1));
/* 165:    */     }
/* 166:167 */     return null;
/* 167:    */   }
/* 168:    */   
/* 169:    */   @NotNull
/* 170:    */   public XmlElementDescriptor[] getRootElementsDescriptors(@Nullable XmlDocument paramXmlDocument)
/* 171:    */   {
/* 172:173 */     if (this.f == null)
/* 173:    */     {
/* 174:174 */       XmlElementDescriptor[] tmp10_7 = XmlElementDescriptor.EMPTY_ARRAY;
/* 175:174 */       if (tmp10_7 == null) {
/* 176:174 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/model/descriptors/RngNsDescriptor", "getRootElementsDescriptors" }));
/* 177:    */       }
/* 178:174 */       return tmp10_7;
/* 179:    */     }
/* 180:177 */     List localList = ChildElementFinder.find(-1, this.f); XmlElementDescriptor[] 
/* 181:178 */       tmp59_56 = convertElementDescriptors(localList);
/* 182:178 */     if (tmp59_56 == null) {
/* 183:178 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/model/descriptors/RngNsDescriptor", "getRootElementsDescriptors" }));
/* 184:    */     }
/* 185:178 */     return tmp59_56;
/* 186:    */   }
/* 187:    */   
/* 188:    */   XmlElementDescriptor[] convertElementDescriptors(List<DElementPattern> paramList)
/* 189:    */   {
/* 190:182 */     paramList = ContainerUtil.findAll(paramList, NamedPatternFilter.INSTANCE);
/* 191:    */     
/* 192:184 */     HashMap localHashMap = new HashMap();
/* 193:185 */     for (Object localObject1 = paramList.iterator(); ((Iterator)localObject1).hasNext();)
/* 194:    */     {
/* 195:185 */       localObject2 = (DElementPattern)((Iterator)localObject1).next();
/* 196:186 */       for (localObject3 = ((DElementPattern)localObject2).getName().listNames().iterator(); ((Iterator)localObject3).hasNext();)
/* 197:    */       {
/* 198:186 */         localObject4 = (QName)((Iterator)localObject3).next();
/* 199:187 */         localObject5 = (List)localHashMap.get(localObject4);
/* 200:188 */         if (localObject5 == null)
/* 201:    */         {
/* 202:189 */           localObject5 = new ArrayList();
/* 203:190 */           localHashMap.put(localObject4, localObject5);
/* 204:    */         }
/* 205:192 */         if (!((List)localObject5).contains(localObject2)) {
/* 206:192 */           ((List)localObject5).add(localObject2);
/* 207:    */         }
/* 208:    */       }
/* 209:    */     }
/* 210:    */     Object localObject3;
/* 211:    */     Object localObject4;
/* 212:    */     Object localObject5;
/* 213:196 */     localObject1 = new ArrayList();
/* 214:198 */     for (Object localObject2 = localHashMap.keySet().iterator(); ((Iterator)localObject2).hasNext();)
/* 215:    */     {
/* 216:198 */       localObject3 = (QName)((Iterator)localObject2).next();
/* 217:199 */       localObject4 = (List)localHashMap.get(localObject3);
/* 218:200 */       localObject5 = a((QName)localObject3, (List)localObject4);
/* 219:201 */       if (localObject5 != null) {
/* 220:202 */         ((List)localObject1).add(localObject5);
/* 221:    */       }
/* 222:    */     }
/* 223:206 */     return (XmlElementDescriptor[])((List)localObject1).toArray(new XmlElementDescriptor[((List)localObject1).size()]);
/* 224:    */   }
/* 225:    */   
/* 226:    */   protected XmlElementDescriptor initDescriptor(@NotNull XmlElementDescriptor paramXmlElementDescriptor)
/* 227:    */   {
/* 228:210 */     if (paramXmlElementDescriptor == null) {
/* 229:210 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/model/descriptors/RngNsDescriptor", "initDescriptor" }));
/* 230:    */     }
/* 231:210 */     return paramXmlElementDescriptor;
/* 232:    */   }
/* 233:    */   
/* 234:    */   @NotNull
/* 235:    */   public XmlFile getDescriptorFile()
/* 236:    */   {
/* 237:215 */     XmlFile tmp4_1 = this.c;
/* 238:215 */     if (tmp4_1 == null) {
/* 239:215 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/model/descriptors/RngNsDescriptor", "getDescriptorFile" }));
/* 240:    */     }
/* 241:215 */     return tmp4_1;
/* 242:    */   }
/* 243:    */   
/* 244:    */   public boolean isHierarhyEnabled()
/* 245:    */   {
/* 246:219 */     return false;
/* 247:    */   }
/* 248:    */   
/* 249:    */   public synchronized PsiElement getDeclaration()
/* 250:    */   {
/* 251:223 */     if (((!this.d.isValid()) || (!this.c.isValid())) && 
/* 252:224 */       (this.e != null))
/* 253:    */     {
/* 254:225 */       VirtualFile localVirtualFile = VirtualFileManager.getInstance().findFileByUrl(this.e);
/* 255:226 */       if (localVirtualFile != null)
/* 256:    */       {
/* 257:227 */         PsiFile localPsiFile = this.g.findFile(localVirtualFile);
/* 258:228 */         if ((localPsiFile instanceof XmlFile)) {
/* 259:229 */           init(((XmlFile)localPsiFile).getDocument());
/* 260:    */         }
/* 261:    */       }
/* 262:    */     }
/* 263:234 */     return this.c.isValid() ? this.c.getDocument() : null;
/* 264:    */   }
/* 265:    */   
/* 266:    */   @NonNls
/* 267:    */   public String getName(PsiElement paramPsiElement)
/* 268:    */   {
/* 269:239 */     return getName();
/* 270:    */   }
/* 271:    */   
/* 272:    */   @NonNls
/* 273:    */   public String getName()
/* 274:    */   {
/* 275:244 */     return getDescriptorFile().getName();
/* 276:    */   }
/* 277:    */   
/* 278:    */   public Object[] getDependences()
/* 279:    */   {
/* 280:248 */     if (this.f != null)
/* 281:    */     {
/* 282:249 */       if (DumbService.isDumb(this.d.getProject())) {
/* 283:250 */         return new Object[] { ModificationTracker.EVER_CHANGED, ExternalResourceManager.getInstance() };
/* 284:    */       }
/* 285:252 */       Object[] arrayOfObject = { this.d, ExternalResourceManager.getInstance() };
/* 286:253 */       PsiElementProcessor.CollectElements localCollectElements = new PsiElementProcessor.CollectElements();
/* 287:254 */       RelaxIncludeIndex.processForwardDependencies(this.c, localCollectElements);
/* 288:255 */       if (localCollectElements.getCollection().size() > 0) {
/* 289:256 */         return ArrayUtil.mergeArrays(arrayOfObject, localCollectElements.toArray());
/* 290:    */       }
/* 291:258 */       return arrayOfObject;
/* 292:    */     }
/* 293:261 */     return new Object[] { ModificationTracker.EVER_CHANGED };
/* 294:    */   }
/* 295:    */   
/* 296:    */   public synchronized void init(PsiElement paramPsiElement)
/* 297:    */   {
/* 298:265 */     this.d = paramPsiElement;
/* 299:266 */     this.c = ((paramPsiElement instanceof XmlFile) ? (XmlFile)paramPsiElement : (XmlFile)paramPsiElement.getContainingFile());
/* 300:267 */     this.g = this.c.getManager();
/* 301:    */     
/* 302:269 */     VirtualFile localVirtualFile = this.c.getVirtualFile();
/* 303:270 */     if (localVirtualFile != null) {
/* 304:271 */       this.e = localVirtualFile.getUrl();
/* 305:    */     }
/* 306:274 */     this.f = RngParser.getCachedPattern(getDescriptorFile(), RngParser.DEFAULT_HANDLER);
/* 307:    */   }
/* 308:    */   
/* 309:    */   public void validate(@NotNull PsiElement paramPsiElement, @NotNull Validator.ValidationHost paramValidationHost)
/* 310:    */   {
/* 311:278 */     if (paramPsiElement == null) {
/* 312:278 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/model/descriptors/RngNsDescriptor", "validate" }));
/* 313:    */     }
/* 314:278 */     if (paramValidationHost == null) {
/* 315:278 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/intellij/plugins/relaxNG/model/descriptors/RngNsDescriptor", "validate" }));
/* 316:    */     }
/* 317:278 */     XmlDocument localXmlDocument = (XmlDocument)PsiTreeUtil.getContextOfType(paramPsiElement, XmlDocument.class, false);
/* 318:279 */     if (localXmlDocument == null) {
/* 319:280 */       return;
/* 320:    */     }
/* 321:282 */     XmlTag localXmlTag = localXmlDocument.getRootTag();
/* 322:283 */     if (localXmlTag == null) {
/* 323:284 */       return;
/* 324:    */     }
/* 325:287 */     if (!"http://relaxng.org/ns/structure/1.0".equals(localXmlTag.getNamespace())) {
/* 326:288 */       XmlInstanceValidator.doValidation(localXmlDocument, paramValidationHost, getDescriptorFile());
/* 327:    */     }
/* 328:    */   }
/* 329:    */   
/* 330:    */   public XmlElementDescriptor getElementDescriptor(String paramString1, String paramString2)
/* 331:    */   {
/* 332:294 */     final QName localQName = new QName(paramString2, paramString1);
/* 333:295 */     CachedValue localCachedValue = (CachedValue)this.a.get(localQName);
/* 334:296 */     if (localCachedValue == null)
/* 335:    */     {
/* 336:297 */       localCachedValue = CachedValuesManager.getManager(this.d.getProject()).createCachedValue(new CachedValueProvider()
/* 337:    */       {
/* 338:    */         public CachedValueProvider.Result<XmlElementDescriptor> compute()
/* 339:    */         {
/* 340:300 */           XmlElementDescriptor localXmlElementDescriptor = RngNsDescriptor.this.a(localQName);
/* 341:301 */           return localXmlElementDescriptor != null ? new CachedValueProvider.Result(localXmlElementDescriptor, localXmlElementDescriptor.getDependences()) : new CachedValueProvider.Result(null, RngNsDescriptor.this.getDependences());
/* 342:    */         }
/* 343:301 */       }, false);
/* 344:    */       
/* 345:    */ 
/* 346:    */ 
/* 347:    */ 
/* 348:306 */       this.a.put(localQName, localCachedValue);
/* 349:    */     }
/* 350:308 */     return (XmlElementDescriptor)localCachedValue.getValue();
/* 351:    */   }
/* 352:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.model.descriptors.RngNsDescriptor

 * JD-Core Version:    0.7.0.1

 */